local skill = fk.CreateSkill {
  name = "emo__enemy_at_the_gates_skill",
}

skill:addEffect("cardskill", {
  prompt = "#emo__enemy_at_the_gates_skill",
  target_num = 1,
  mod_target_filter = function(self, player, to_select, selected, card)
    return to_select ~= player
  end,
  target_filter = Util.CardTargetFilter,
  on_effect = function(self, room, effect)
    local player = effect.from
    local target = effect.to
    effect.extra_data = effect.extra_data or {}
    local source_cards = effect.extra_data.fromIds or {} -- 额外的来源牌
    local n = 4 + (effect.extra_data.num or 0) -- 额外亮出数
    if n < 1 and #source_cards == 0 then return end
    local cards = #source_cards > 0 and source_cards or room:getNCards(n, effect.extra_data.fromArea or "top")
    effect.extra_data.EMOenemy_at_the_gates = cards
    -- 在此移动时机，可以修改亮出牌的信息……
    room:moveCardTo(cards, Card.Processing, nil, fk.ReasonJustMove, self.name, nil, true, player)
    cards = effect.extra_data.EMOenemy_at_the_gates
    local rest = {}
    while not target.dead do
      cards = table.filter(cards, function (id)
        return room:getCardArea(id) == Card.Processing
      end)
      if #cards == 0 then break end
      room:delay(600)
      local cid = table.remove(cards, 1)
      local card = Fk:getCardById(cid)
      if card.trueName == "slash" and player:canUseTo(card, target, {bypass_distances = true, bypass_times = true}) then
        local use = UseCardData:new {
          card = card,
          from = player,
          tos = {target},
          extraUse = true,
        }
        room:useCard(use)
        if use.damageDealt and use.damageDealt[target] then
          -- 记录所有亮出的造成伤害的杀和伤害值
          local useParent = room.logic:getCurrentEvent():findParent(GameEvent.UseCard)
          if useParent then
            local use_data = useParent.data
            use_data.extra_data = use_data.extra_data or {}
            use_data.extra_data.enemy_at_the_gates_damageInfo = use_data.extra_data.enemy_at_the_gates_damageInfo or {}
            table.insert(use_data.extra_data.enemy_at_the_gates_damageInfo, {card, use.damageDealt[target]})
          end
        end
      else
        table.insert(rest, cid)
      end
    end
    rest = table.filter(rest, function (id) return room:getCardArea(id) == Card.Processing end)
    if #rest > 0 then
      local toArea = effect.extra_data.toArea -- 剩余牌放回的区域
      if toArea == "top" then
        room:moveCards({
          ids = table.reverse(rest), toArea = Card.DrawPile, moveReason = fk.ReasonPut,
        })
      elseif toArea == "bottom" then
        room:moveCards({
          ids = rest, toArea = Card.DrawPile, moveReason = fk.ReasonPut, drawPilePosition = -1,
        })
      elseif effect.extra_data.toPlayer and type(effect.extra_data.toPlayer) == "table"
      and not effect.extra_data.toPlayer.dead then -- 交给某名角色
        room:moveCards({
          ids = rest, to = effect.extra_data.toPlayer, toArea = Card.PlayerEquip, moveReason = fk.ReasonJustMove,
        })
      else -- 默认置入弃牌堆
        room:moveCards({
          ids = rest,
          toArea = Card.DiscardPile,
          moveReason = fk.ReasonPutIntoDiscardPile,
        })
      end
    end
  end,
})



return skill
